package com.cheng.im.client;

import com.cheng.im.client.controller.CommandController;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Configuration;

@Slf4j
@Configuration
@SpringBootApplication
public class ClientApplication {

    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(ClientApplication.class);


        //  jvm选项    -Xms2G -Xmx8G
        //  命令 参数   soultest1 10 1000000 1
        //  命令 参数   soultest1 10 10 1000
        //  第一个参数为 soultest1， 启动百万连接 netty 灵魂测试客户端， 第二个为执行的间隔， 第三个是启动多少个客户端连接， 第四个是从几号id用户开始创建连接

        if (args.length > 0 && (args[0] != null && args[0].equalsIgnoreCase("soultest1"))) {
            startSoultest1Client(context, args);
        } else {
            CommandController commandClient = context.getBean(CommandController.class);

            commandClient.initCommandMap();
            try
            {
                commandClient.startCommandThread();

            } catch (InterruptedException e)
            {
                e.printStackTrace();
            }
        }
    }

    private static void startSoultest1Client(ApplicationContext context, String[] args) {

        SoulTest1Client soulTest1Client = context.getBean(SoulTest1Client.class);

        if (args.length > 1 && args[1] != null) {
            // 请求的时间间隔，单位为ms
            SoulTest1Client.serverPortCount = Long.parseLong(args[1]);
        }
        if (args.length > 2 && args[2] != null) {
            // 连接上限
            SoulTest1Client.maxConnection = Long.parseLong(args[2]);
        }
        if (args.length > 3 && args[3] != null) {
            // 连接上限
            SoulTest1Client.startUserID = Long.parseLong(args[3]);
        }

        soulTest1Client.doConnect();
    }
}
